package com.sample.springbatch.job.sample.reader;

import com.sample.springbatch.job.mapper.WrappedJsonLineMapper;
import com.sample.springbatch.model.HbBillLoanDetailEntity;
import com.sample.springbatch.model.RepayOrderInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.LineCallbackHandler;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.separator.JsonRecordSeparatorPolicy;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.PathResource;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * Flat文件格式读取类  实现了分隔符和json格式
 * Created by zwg on 2017/8/8.
 */
@Component
public class HbBillLoanDetailFlatFileItemReader<T> extends FlatFileItemReader {

    Logger logger = LoggerFactory.getLogger(HbBillLoanDetailFlatFileItemReader.class);

    public HbBillLoanDetailFlatFileItemReader() {
        //System.out.println(" ====================== times ========="+times);
        repayOrderOfDelimitedLine();
    }


    /**
     * 分隔符文件读取配置
     */
    private void repayOrderOfDelimitedLine() {
        //setResource(new ClassPathResource("jobdata/bill_loan_detail.dat")); //"jobdata/repay-orders.dat")); //jobdata/repay-orders-big.dat
        setLineMapper(new DefaultLineMapper<HbBillLoanDetailEntity>() {{
            setLineTokenizer(new DelimitedLineTokenizer(",") {{  //一行内容分割默认用','分割
                setNames(new String[] { "contract_no","fund_seq_no","prod_code","internal_transfer_tag","contract_type","name","cert_type","cert_no","loan_status","loan_use","use_area","apply_date","currency","encash_amt","total_terms","encash_date","start_date","end_date","repay_mode","grace_day","total_fee_rate","rate_type","day_rate","prin_repay_frequency","int_repay_frequency","guarantee_type","credit_no","encash_acct_type","encash_acct_no","repay_acct_type","repay_acct_no","apply_no","agreement_no" });
                setComments(new String[] { "==","##" });  //# 为默认的注释符号
                setLinesToSkip(1); //字段说明
            }});
            setFieldSetMapper(new BeanWrapperFieldSetMapper<HbBillLoanDetailEntity>() {{
                setTargetType(HbBillLoanDetailEntity.class);
            }});
            setSkippedLinesCallback(new LineCallbackHandler() {
                @Override
                public void handleLine(String s) {
                    logger.error("skip line:"+s);
                }
            });

        }});
    }

    @Override
    protected void doOpen() throws Exception {
        //路径需要通过配置参数类获取
        String path = "D:/javawk/Sample-SpringBatch/src/main/resources/jobdata";
        String filePrefix = "bill_loan_detail";
        File pathFile = new File(path);

        if (!pathFile.exists()){
            throw new Exception("文件路径不存在,路径:"+path);
        }

        String[] fileNames = pathFile.list();
        if(fileNames==null && fileNames.length<=0) {
            throw new Exception("文件"+filePrefix+"不存在,路径:"+path);
        }
        String filePath = "";
        for(String fileName:fileNames){
            if(fileName.startsWith(filePrefix)){
               filePath= path+File.separator+fileName;
            }
        }

        setResource(new PathResource(filePath));
        //setResource(new ClassPathResource(filePath));

        super.doOpen();
    }
}
